<template>
  <div class="artist">
    <div class="w1200">
      <!-- filter -->
      <div class="filter">
        <div class="filter-item">
          <span
            :class="index === initialIndex ? 'active' : ''"
            v-for="(initial, index) in initial"
            :key="index"
            @click="selectType('initial', index)"
            >{{ initial.label }}
          </span>
        </div>
        <div class="filter-item">
          <span
            :class="index === areaIndex ? 'active' : ''"
            v-for="(area, index) in area"
            :key="index"
            @click="selectType('area', index)"
            >{{ area.label }}</span
          >
        </div>
        <div class="filter-item">
          <span
            :class="index === typeIndex ? 'active' : ''"
            v-for="(type, index) in type"
            :key="index"
            @click="selectType('type', index)"
            >{{ type.label }}</span
          >
        </div>
      </div>
      <!-- class="list-container" -->
        <div
          class="list-container"
         ref="list"

        >
          <router-link
            :to="{ path: '/singer', query: { id: singer.id } }"
            class="item"
            v-for="singer in list"
           :key="singer.id"
          >
            <div class="faceImg">
              <el-image :src="singer.img1v1Url" class="img" lazy>

              </el-image>
            </div>
            <div class="info">
              <div class="name">{{ singer.name }}</div>
              <div class="albumSize">专辑：{{ singer.albumSize }}</div>
            </div>
          </router-link>
        </div>

    </div>
  </div>
</template>

<script>
import assents from "../../assets/css/global.css";
import { reqGetArtist} from "../../api/singer";
export default {
  name: "Artist",

  data() {
    //用于存放所有数据
    return {
      //热门排序分类 initial:首字母
      initial: [
        { label: "热门", val: -1 },
        { label: "#", val: 0 },
      ],
      //歌曲语言分类 area:地区
      area: [
        { label: "全部", val: -1 },
        { label: "华语", val: 7 },
        { label: "欧美", val: 96 },
        { label: "日本", val: 8 },
        { label: "韩国", val: 16 },
        { label: "其他", val: 0 },
      ],
      //歌手分类
      type: [
        { label: "全部", val: -1 },
        { label: "男歌手", val: 1 },
        { label: "女歌手", val: 2 },
        { label: "乐队", val: 3 },
      ],
      //初始化三个分类的id为0
      initialIndex: 0,
      areaIndex: 0,
      typeIndex: 0,
      //参数
      params: {
        initial: "",
        area: "",
        type: "",
        limit: 24,
        offset: 0,
      },
      //singerDetail
      singerDetail: {},
      //list
      list: [],
      isLoadMv: true,
    };
  },

  async mounted() {
    this.params.initial = this.initial[this.initialIndex].val;
    this.params.area = this.area[this.areaIndex].val;
    this.params.type = this.type[this.typeIndex].val;
    //遍历26个字母的方法
    this.traverseInitial();
    this.getArtist(this.params);
    this.awaysScroll();
  },
  //方法
  methods: {
    //此方法用于实现26个大写字母
    traverseInitial() {
      //遍历出来26个字母
      const initial = [];
      for (let i = 0; i < 26; i++) {
        initial.push({
          label: String.fromCharCode(65 + i),
          val: String.fromCharCode(97+i),
        });
      }
       this.initial = [this.initial[0], ...initial, this.initial[1]];

      // //console.log(initial);
    },
    //单击切换
    selectType(type, index) {
      this[type + "Index"] = index;
      this.list = [];
      this.params.offset = 0;
      this.params[type] = this[type][index].val;
      // //console.log(type, index);
    },
    async getArtist(params) {
      const res = await reqGetArtist(params);
      // //console.log("你点击了哦我", res);
      /*   if (res.code !== 200) {
        return this.$msg.error("数据请求失败");
      } */
      this.list =this.params.offset !== 0 ? [...this.list, ...res.artists] : res.artists;
      this.isLoadMv = !res.more;

    },
   loadMore() {
      this.isLoadMv = true;
      this.params.offset = this.list.length;
    },
     // 无限下拉
      awaysScroll() {
        let bottom = null;
        let isfinish = null;
        const height = document.documentElement.clientHeight;
        window.onscroll = async () => {
          bottom = this.$refs.list.getBoundingClientRect().bottom;
          if (bottom <= height + 20) {
            if (isfinish) {
              // console.log("没加载完成");
              return;
            }
            this.params.offset += 30;
            isfinish = true;
            // console.log("加载中", bottom);
            const res = await this.getArtist(this.params);
            // console.log(res);
           /*  this.list = this.list.concat(res); */
            isfinish = false;
          }
        };
      },
  },
  watch: {
    params: {
      handler(params) {
        this.getArtist(params);
      },
      deep: true, // 深度监听
    },
  },

  componts: {
    assents,
  },
   beforeDestroy() {
      window.onscroll = "";
    },
};
</script>

<style lang="less" scoped>
.filter {
  padding: 40px 0 20px;
  position: relative;
}
.filter-item {
  font-size: 0;
  padding-bottom: 20px;
  span {
    display: inline-block;
    padding: 0 12px;
    font-size: 14px;
    line-height: 30px;
    margin-right: 6px;
    vertical-align: top;
    cursor: pointer;
    border-radius: 3px;
    &.active {
      color: #fff;
      background: #ff641e;
    }
  }
}
/* .w {
  display: flex;
  flex-wrap: wrap;
  height: 100vh;
  overflow-y: auto;
} */
.list-container {
  display: flex;
  flex-wrap: wrap;
  padding-bottom: 40px;

  .item {
    width: 120px;
    margin: 0 40px 40px;
    .img {
      transition: all 0.4s linear;
      img {
        width: 100%;
        height: 100%;
      }
    }
    &:hover {
      .img {
        transform: rotateY(180deg);
      }
    }
  }
  .faceImg {
    width: 120px;
    height: 120px;
    border-radius: 100%;
    overflow: hidden;
  }
  .info {
    text-align: center;

    .name {
      line-height: 28px;
      font-size: 14px;
    }

    .albumSize {
      color: #999;
    }
  }
}
</style>
